package net.howson.phil.datahiding.stream;

import java.io.IOException;
import java.io.OutputStream;

public class NibbleOutputStream extends OutputStream {

	private int[] buffer = new int[2];
	private final OutputStream delegate;
	private int cursor;
	
	public NibbleOutputStream(OutputStream delegate) {
		this.delegate = delegate;
	}



	@Override
	public void write(int b) throws IOException {
		buffer[cursor++] = b;
		if (cursor == buffer.length) {
			delegate.write(buffer[0]<<4 | buffer[1]);
			cursor = 0;
		}
	}
	
	@Override
	public void close() throws IOException {
		delegate.close();
		super.close();
	}

}
